package com.sgt.frp.utils;

import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.sgt.frp.config.LinuxConfig;
import com.sgt.frp.excepiton.BusinessException;

import java.io.ByteArrayOutputStream;

public class SshUtil {
    private static Session session;

    public static void exec(LinuxConfig linuxConfig, String cmd) throws JSchException {
        session= JschUtil.getSession(linuxConfig.getHost(), linuxConfig.getPort(), linuxConfig.getUsername(), linuxConfig.getPassword());
        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        channelExec.setCommand(cmd);
        channelExec.setOutputStream(out);
        try {
            channelExec.connect();
        }catch (JSchException e){
            throw BusinessException.init("ssh连接失败");
        }
    }

}
